package com.example.campuslink.ui.activity

import android.content.Intent
import android.provider.MediaStore
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.ProvinceAdapter
import com.example.campuslink.commen.adpter.SchoolAdapter
import com.example.campuslink.commen.dialog.AddSchoolDialog
import com.example.campuslink.commen.tools.PermissionManager
import com.example.campuslink.commen.tools.SpacesItemButtonDecoration
import com.example.campuslink.databinding.ActivitySchoolChoiceBinding
import com.example.campuslink.viewModel.SchoolChoiceViewModel

class SchoolChoiceActivity : BaseActivity<ActivitySchoolChoiceBinding, SchoolChoiceViewModel>(
    SchoolChoiceViewModel::class.java,
    R.layout.activity_school_choice,
    BR.viewModel
) {
    private lateinit var provinceAdapter: ProvinceAdapter
    private val PICK_IMAGE_REQUEST = 1
    private lateinit var imgPath: String
    private lateinit var addSchoolDialog: AddSchoolDialog
    private lateinit var schoolAdapter: SchoolAdapter

    private val permissionManager = PermissionManager(this)

    override fun initView() {
        mViewModel?.context = this
        mViewModel?.initDB(this)
        mViewModel?.haveSchool()
        provinceAdapter = ProvinceAdapter(mViewModel!!)
        schoolAdapter = SchoolAdapter(mViewModel!!)
        mViewDataBinding?.rvProvince?.adapter = provinceAdapter
        mViewDataBinding?.rvProvince?.layoutManager = GridLayoutManager(this, 8)
        if (mViewModel?.provinceData?.isNotEmpty()!!) {
            provinceAdapter.setListData(mViewModel?.provinceData!!)
        }
        mViewModel?.setProvinceIndex(0)
        mViewModel?.isAdmin()
        mViewModel?.isAdmin?.observe(this) {
            if (it) {
                mViewDataBinding?.btnAddSchool?.visibility = View.VISIBLE
                mViewDataBinding?.btnAddSchool?.isEnabled = true
            } else {
                mViewDataBinding?.btnAddSchool?.visibility = View.GONE
                mViewDataBinding?.btnAddSchool?.isEnabled = false
            }
        }
//        notifySchoolAdapter(0)
        mViewDataBinding?.freshLayout?.setOnRefreshListener(object :
            SwipeRefreshLayout.OnRefreshListener {
            override fun onRefresh() {

                notifySchoolAdapter()

                mViewDataBinding?.freshLayout?.isRefreshing = false
            }

        })
        mViewDataBinding?.btnAddSchool?.setOnClickListener {
            addSchoolDialog = AddSchoolDialog()
            addSchoolDialog.show(supportFragmentManager, "addSchoolDialog")
        }

        mViewModel?.provinceIndex?.observe(this) {
            notifySchoolAdapter()
        }
        mViewDataBinding?.rvChoiceSchools?.adapter = schoolAdapter
        mViewDataBinding?.rvChoiceSchools?.layoutManager = LinearLayoutManager(this)
        mViewDataBinding?.rvChoiceSchools?.addItemDecoration(SpacesItemButtonDecoration(15))
        mViewModel?.schoolLiveData?.observe(this) {
            schoolAdapter.setListData(it)
        }
    }

    fun openGallery() {
        permissionManager.requestReadStoragePermission(this) {
            val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(intent, PICK_IMAGE_REQUEST)
        }
    }

    fun notifySchoolAdapter() {
        mViewModel?.getProvinceSchool(mViewModel?.provinceIndex?.value ?: 0)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            val selectedImageUri = data.data
            val projection = arrayOf(MediaStore.Images.Media.DATA)
            val contentResolver = contentResolver
            val cursor = contentResolver.query(selectedImageUri!!, projection, null, null, null)

            if (cursor != null) {
                cursor.moveToFirst()
                val columnIndex = cursor.getColumnIndex(projection[0])
                val imagePath = cursor.getString(columnIndex)
                cursor.close()

                // 此时imagePath就是所选图片的字符串形式的存储路径，可以在这里进行后续操作，如加载图片等
                imgPath = imagePath
            }
            addSchoolDialog.imgPath = imgPath
            addSchoolDialog.upload()
        }
    }
}